﻿using Hotel.BLL;
using Hotel.Model.House;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;



namespace Hotel.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HouseController : ControllerBase
    {
        HouseBLL bll;
        public HouseController(HouseBLL bll)
        {
            this.bll = bll;
        }

        /// <summary>
        /// 房型的增加
        /// </summary>
        /// <param name="info">房型的实体</param>
        /// <returns>影响的行数</returns>
        [HttpPost]
        public IActionResult PostHouseTypeAdd(T_House_Type info)
        {
            try
            {
               int res = bll.PostHouseTypeAdd(info);
                return Ok(new
                {
                    data = res
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有的房型
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult   GetHouseTypeList( int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //获取所有显示的数据
                 int totalPage = 0;
                 int totalCount = 0;
                List<T_House_Type> list = bll.GetHouseTypeList(out totalPage, out totalCount, pageIndex, pageSize);

                return Ok(new
                {
                    data = list,
                    totalPage = totalPage,
                    totalCount = totalCount
                    
                });

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id获取数据  实现数据的反填
        /// </summary>
        /// <param name="HtId">房型id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHouseTypeById(int HtId)
        {
            try
            {
                // T_House_Type  info = db.T_House_Type.Where(c=>c.HY_Id == HtId).FirstOrDefault();
                T_House_Type info = bll.GetHouseTypeById(HtId);
                return Ok(new
                {
                    data = info,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id进行修改某一条数据（对象）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PutHouseTypeById(T_House_Type info)
        {
            try
            {
                int res = bll.PutHouseTypeById(info);
                return Ok(new
                {
                    data = res,
                });
               
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id修改房型状态
        /// </summary>
        /// <param name="HTid"></param>
        /// <param name="HTstate"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PutHouseTypeStateById(int HTid, int HTstate)
        {
            try
            {
                int res = bll.PutHouseTypeStateById(HTid, HTstate);
                return Ok(new
                {
                    data = res,
                });
               
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id进行删除一条记录
        /// </summary>
        /// <param name="HTid">房型id</param>
        /// <returns></returns>

        [HttpDelete]
        public IActionResult DeleteHouseTypeById(int HTid)
        {
            try
            {
                int res = bll.DeleteHouseTypeById(HTid);
                return Ok(new
                {
                    data = res,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 实现批量删除
        /// </summary>
        /// <param name="idsArr"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteAllHouseTypeById(string idsArr)
        {
            try
            {
                int res = bll.DeleteAllHouseTypeById(idsArr);
                return Ok(new
                {
                    data = res,
                });

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取房态
        /// </summary>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult GetHouseStateList()
        {
            try
            {
                List<T_House_State> list = bll.GetHouseStateList();
                return Ok(new
                {
                    data = list,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加房间号
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddHouseNo(T_House_No info)
        {
            try
            {
                //1.先判断一下房间号重复不重复
                int res = bll.PostAddHouseNo(info);
                return Ok(new { data = res });
              
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房间号分页
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="houseNo">房间号</param>
        /// <param name="htId">房型</param>
        /// <param name="storeId">门店</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHouseNoInfos( string? houseNo = "", int? htId = null, int? storeId = null, int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                int totalPage = 0;
                int totalCount = 0;
                //1.查询所有的房间
                var list = bll.GetHouseNoInfos(out totalPage, out totalCount, houseNo, htId, storeId, pageIndex, pageSize);

                return Ok(new
                {
                    data = list,
                    totalPage = totalPage,
                    totalCount = totalCount,
                });

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取所有的空闲房间
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHouseState(int? htId = null, int? storeId = null)
        {
            try
            {
                var list = bll.GetHouseState(htId,storeId);

                return Ok(new
                {
                    data = list,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 找某个门店下的房间变成已入住就行
        /// </summary>
        /// <param name="hsId"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHouseNoStateStore(int hsId, int storeId)
        {
            try
            {
                var res = bll.GetHouseNoStateStore(hsId, storeId);
                return Ok(new
                {
                    data = res,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加房价计划
        /// </summary>
        /// <param name="info">房价计划实体类</param>
        /// <returns>返回影响的行数</returns>
        [HttpPost]
        public IActionResult PostAddHousePrice(T_House_Price info)
        {
            try
            {
               int res = bll.PostAddHousePrice(info);
                return Ok(new
                {
                    data = res,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据房型、房价计划名称  开始时间和结束时间  分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHousePriceList()
        {
            try
            {
                var list = bll.GetHousePriceList();
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 房间删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteHouseNo(int id)
        {
            try
            {
                int res = bll.DeleteHouseNo(id);
                return Ok(new
                {
                    data = res,
                });
                
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改房间的状态
        /// </summary>
        /// <param name="houseId"></param>
        /// <param name="houseStateId"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PutHouseNoState(int houseId, int houseStateId)
        {
            try
            {
                int res = bll.PutHouseNoState(houseId, houseStateId);
                return Ok(new
                {
                    data = res
                });
               
            }
            catch (Exception)
            {

                throw;
            }
        }


       /// <summary>
       /// 导出房间号
       /// </summary>
       /// <returns></returns>

        [HttpGet]
        public ActionResult ExportExcel()
        {
            try
            {
                //导出的数据
                byte[] data = null;
                //创建工作薄的类型
                string contentType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                //文件的名称
                string fileName = "房间号列表.xlsx";

                //创建工作薄
                IWorkbook book = new XSSFWorkbook();
                //工作表
                ISheet sheet = book.CreateSheet();
                //创建第一行
                IRow row1 = sheet.CreateRow(0);

                //给第一行指定标题
                row1.CreateCell(0).SetCellValue("ID");
                row1.CreateCell(1).SetCellValue("房间号");
                row1.CreateCell(2).SetCellValue("房型");
                row1.CreateCell(3).SetCellValue("房态");
                row1.CreateCell(4).SetCellValue("房间图片");
                row1.CreateCell(5).SetCellValue("门店");

                int totalPage = 0;
                int totalCount = 0;
                var list = bll.GetHouseNoInfos(out totalPage, out totalCount);
                int rowIndex = 1;

                foreach (var item in list)
                {
                    IRow row = sheet.CreateRow(rowIndex);
                    row.CreateCell(0).SetCellValue(item.HN_Id);
                    row.CreateCell(1).SetCellValue(item.HN_Name);
                    row.CreateCell(2).SetCellValue(item.HY_Name);
                    row.CreateCell(3).SetCellValue(item.HS_Name);
                    row.CreateCell(4).SetCellValue(item.HY_Photo);
                    row.CreateCell(5).SetCellValue(item.StoreId);

                    rowIndex++;
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    //工作表 写入内容流中
                    book.Write(ms);

                    //内存流 赋给 数据字节数组中
                    data = ms.ToArray();
                }
                //注释： File是控制器自带方法，不是IO流中的File  // 返回一个文件结果
                return File(data, contentType, fileName);

            }


            catch (Exception)
            {

                throw;
            }
        }
    }
}
